package com.zhangjie.demo.controller;

import com.zhangjie.demo.util.RedisUtil;
import com.zhangjie.demo.entity.Result;
import com.zhangjie.demo.entity.User;
import com.zhangjie.demo.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: ZhangJie
 * @Description: 控制层
 * @Date: 2023/07/02
 */
@RestController
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RedisUtil redisUtil;


    /***
     * @Description: 根据id插入用户信息
     * @Param: [id]
     * @return: boolean
     * @Author: zhangjie
     * @Date: 2023/7/2
     */
    @GetMapping("/sendMsg/{id}")
    public Result<User> insertById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        String userName = user.getName();
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString();
        String key = userName + uuidStr;
        redisUtil.setNx(key, userName, 30L, TimeUnit.DAYS);
        return Result.ok();
    }

    /***
     * @Description: 根据id统计用户30天内当前收到多少信息
     * @Param: [id]
     * @return: com.zhangjie.demo.entity.Result<com.zhangjie.demo.entity.User>
     * @Author: ZhangJie
     * @Date: 2023/7/2
     */
    @GetMapping("record/{id}")
    public Result<User> count(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        String userName = user.getName();
        Set<String> listKey = redisUtil.getListKey(userName);
        Integer count = listKey.size();
        return Result.ok(count, user);
    }
}
